package com.xuecheng.content.service.jobHander;

import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.feignclient.CourseIndex;
import com.xuecheng.content.feignclient.SearchServiceClient;
import com.xuecheng.content.mapper.CoursePublishMapper;
import com.xuecheng.content.model.dto.CoursePreviewDto;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.service.CoursePublishService;
import com.xuecheng.messagesdk.model.po.MqMessage;
import com.xuecheng.messagesdk.service.MessageProcessAbstract;
import com.xuecheng.messagesdk.service.MqMessageService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.constraints.Size;
import java.io.File;


@Component
@Slf4j
public class CoursePublishTask extends MessageProcessAbstract {
    @Autowired
    CoursePublishService coursePublishService;
    @Autowired
    CoursePublishMapper coursePublishMapper;
    @Autowired
    SearchServiceClient searchServiceClient;

    @XxlJob("CoursePublishJobHandler")
    public void coursePublishJobHandler() throws Exception {
        // 分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        log.debug("shardIndex="+shardIndex+",shardTotal="+shardTotal);
        //参数:分片序号、分片总数、消息类型、一次最多取到的任务数量、一次任务调度执行的超时时间
        process(shardIndex,shardTotal,"course_publish",30,60);
    }


    //执行课程发布任务逻辑
    @Override
    public boolean execute(MqMessage mqMessage) {
        Long courseId =Long.parseLong(mqMessage.getBusinessKey1());
        //课程页面静态化
        generateCourseHtml(mqMessage,courseId);
        //向redis写缓存
        saveCourseCache(mqMessage,courseId);

        //向es插入索引
        saveCourseIndex(mqMessage,courseId);

        return true;
    }
    public void generateCourseHtml(MqMessage mqMessage,long courseId){
        Long id = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();

        //做任务幂等性处理
        //取出该阶段执行状态
        int stageOne = mqMessageService.getStageOne(id);
        if(stageOne>0){
            log.debug("课程静态化已处理直接返回，课程id:{}",courseId);
        }

        //开始课程静态化
        File file = coursePublishService.generateCourseHtml(courseId);
        if(file==null){
            XueChengPlusException.cast("生成的静态页面为空");
        }
        //将html上传到minio
         coursePublishService.uploadCourseHtml(courseId,file);

        //任务处理为完成
        mqMessageService.completedStageOne(id);
    }
    public void saveCourseCache(MqMessage mqMessage,long courseId){

    }
    //保存课程索引信息
    public void saveCourseIndex(MqMessage mqMessage,long courseId) {
        Long id=mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        int stageTwo = mqMessageService.getStageTwo(id);
        if(stageTwo>0){
            log.debug("课程索引信息已存入，课程id:{}",courseId);
        }
        //查询课程信息，调用搜索服务添加索引接口
        CoursePublish coursePublish = coursePublishMapper.selectById(courseId);
        CourseIndex courseIndex = new CourseIndex();
        BeanUtils.copyProperties(coursePublish,courseIndex);
        Boolean add = searchServiceClient.add(courseIndex);
        if(add==false){
            XueChengPlusException.cast("远程调用搜索服务添加课程索引失败。");
        }
        //完成本阶段任务
        mqMessageService.completedStageTwo(id);
    }
}
